<?php
namespace App\Services\Payment;

use App\Services\CommonConfigService;
use function config_path;
use function request;

/**
 * Class CommonPayService
 * @package app\common\service\pay
 * @Author: TinyMeng <666@majiameng.com>
 * @Created: 2020/8/14
 */
class ConfigService extends CommonConfigService
{
    public $data;      //支付参数
    public $payment;   //支付类型 alipay / wxpay

    /**
     * 加载配置
     * @param $data
     * @return array|false
     * @author: tinymeng <666@majiameng.com>
     * @ctime:  2021/6/7 18:11
     */
    public function loadConfig($data){
        $configCode = $this->configCode;
        $domain = (request()->isSecure()?'https':'http').'://'.request()->getHost();
        if(in_array($configCode,['wechat','alipay'])){
            /** 生成config/pay.php 所需要的配置 */

            foreach ($data as $key=>$value){
                if($key == 'wechat_public_cert_path'){
                    $data[$key] = empty($value)?[]:json_decode($value,true);
                }elseif($key == 'mode'){
                    $data[$key] = (int)$value;
                }elseif(in_array($key ,['mch_secret_cert' , 'mch_public_cert_path'])){
                    $data[$key] = config_path() .$value;
                }elseif(in_array($key ,['app_public_cert_path','alipay_public_cert_path', 'alipay_root_cert_path'])){
                    $data[$key] = config_path() .$value;
                }else{
                    $data[$key] = $value;
                    if(in_array($key ,['mp_app_id','app_id', 'mini_app_id'])){
                        $data['sub_'.$key] = $value;//微信服务商appid
                    }
                }
            }
            $data['notify_url'] = $domain."/manage/payment/notify/$configCode/".$this->company['app_id'];
            $data['return_url'] = $domain."/manage/payment/return/$configCode/".$this->company['app_id'];

            $config = config('pay');
            $config[$configCode]['default'] = array_merge($config[$configCode]['default'],$data);
            return $config;
        }else{
            return $data;
        }
    }
}
